<?php
	require_once("../conexion/conexion.php");
	require_once("../Funciones/funciones_publicas.php");
	extract($_POST);
	$fun = new funciones_publicas();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento sin t&iacute;tulo</title>
<link href="../Estilos/global.css" rel="stylesheet" type="text/css" />
<link href="../Estilos/local.css" rel="stylesheet" type="text/css" />
<link href="../Estilos/CssTabla3.css" rel="stylesheet" type="text/css" />

<script src="../include_js/jquery.js" type="text/javascript"></script>
<style>
.facNormal
{
	background:#D2B48C;
	font-family:Verdana, Arial, Helvetica, sans-serif; 
    font-size:11px;
	text-align:center;
	vertical-align:middle;
	padding:3px;
}
.facExenta
{
	background:#81D371;
	font-family:Verdana, Arial, Helvetica, sans-serif; 
    font-size:11px;
	text-align:center;
	vertical-align:middle;
	padding:3px;
}
.facElectronica
{
	background:#5F9EA0;
	font-family:Verdana, Arial, Helvetica, sans-serif; 
    font-size:11px;
	text-align:center;
	vertical-align:middle;
	padding:3px;
}
.facOtros
{
	background:#66CDAA;
	font-family:Verdana, Arial, Helvetica, sans-serif; 
    font-size:11px;
	text-align:center;
	vertical-align:middle;
	padding:3px;
}
.facCompra
{
	background:#C3D8EB;
	font-family:Verdana, Arial, Helvetica, sans-serif; 
    font-size:11px;
	text-align:center;
	vertical-align:middle;
	padding:3px;
}
.notaCredito
{
	background:#F45740;
	font-family:Verdana, Arial, Helvetica, sans-serif; 
    font-size:11px;
	text-align:center;
	vertical-align:middle;
	padding:3px;
}
.cuadroNormal {
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	margin-left: 0px;
	padding: 0px;
	border: 1px solid #000000;
	background-color:#D2B48C;
	width:15px;
	height:15px;
}
.cuadroExenta {
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	margin-left: 0px;
	padding: 0px;
	border: 1px solid #000000;
	background-color:#81D371;
	width:15px;
	height:15px;
}
.cuadroElectronica {
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	margin-left: 0px;
	padding: 0px;
	border: 1px solid #000000;
	background-color:#5F9EA0;
	width:15px;
	height:15px;
}
.cuadroOtros {
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	margin-left: 0px;
	padding: 0px;
	border: 1px solid #000000;
	background-color:#66CDAA;
	width:15px;
	height:15px;
}
.cuadroCompra {
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	margin-left: 0px;
	padding: 0px;
	border: 1px solid #000000;
	background-color:#C3D8EB;
	width:15px;
	height:15px;
}
.cuadroCredito {
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	margin-left: 0px;
	padding: 0px;
	border: 1px solid #000000;
	background-color:#F45740;
	width:15px;
	height:15px;
}
#barras {
	padding: 0px;
	margin-top: 10px;
	margin-bottom:0px;
	margin-right:0px;
	margin-left:0px;
}

#barras  .cuadroNormal{
	padding-top: 0px;
	padding-left: 0px;
	margin: 0px;
	list-style-type: none;
	display: inline;/*deja alineado el menu horizontalmente*/
	padding-right: 20px;
}
#barras  .cuadroExenta{
	padding-top: 0px;
	padding-left: 0px;
	margin: 0px;
	list-style-type: none;
	display: inline;/*deja alineado el menu horizontalmente*/
	padding-right: 20px;
}
#barras  .cuadroElectronica{
	padding-top: 0px;
	padding-left: 0px;
	margin: 0px;
	list-style-type: none;
	display: inline;/*deja alineado el menu horizontalmente*/
	padding-right: 20px;
}
#barras  .cuadroOtros{
	padding-top: 0px;
	padding-left: 0px;
	margin: 0px;
	list-style-type: none;
	display: inline;/*deja alineado el menu horizontalmente*/
	padding-right: 20px;
}
#barras  .cuadroCompra{
	padding-top: 0px;
	padding-left: 0px;
	margin: 0px;
	list-style-type: none;
	display: inline;/*deja alineado el menu horizontalmente*/
	padding-right: 20px;
}
#barras  .cuadroCredito{
	padding-top: 0px;
	padding-left: 0px;
	margin: 0px;
	list-style-type: none;
	display: inline;/*deja alineado el menu horizontalmente*/
	padding-right: 20px;
}
#barras  .espacio{
	padding-top: 0px;
	padding-left: 0px;
	margin: 0px;
	list-style-type: none;
	display: inline;/*deja alineado el menu horizontalmente*/
	padding-right: 20px;
	float:right;
}
.filaSumatoria
{
	background:#CCCCCC;
	font-family:Verdana, Arial, Helvetica, sans-serif; 
	color:#0066CC;
    font-size:12px;
	text-align:center;
	vertical-align:middle;
	padding:3px;
}
</style>
<script>
$(document).ready(function() {
     $("#exportar").click(function(event) {
     //$("#datos_a_enviar").val( $("<div>").append( $("#tbExportar").eq(0).clone()).html());
	 $("#txtMes").val();
	 $("#txtAgno").val();
     $("#form_compra").submit();
});
});

</script>
</head>

<body>
<form name="form_compra" id="form_compra" method="post" action="Reportes/Excel_Libro_Venta.php">
<?php
$mes = $fun->SeleccionaMes($cmb_Mes);
//factura psmb
$con = new Conexion();
$con2 = $con->conexionBD("fpproduccion");
$sql = "select 
		date_format(FacFecha,'%d/%m/%Y') as fecha,
		FacCod,
		clirsocial,
		factura.clirut,
		FacNeto,
		FacIva,
		FacTotal,
		FacEstado
		
		from factura
		inner join clientes on
		clientes.clirut=factura.clirut
		where month(facfecha)='".$cmb_Mes."' and year(facfecha)='".$cmb_Agno."' and facestado!='ANULADA'
		order by facfecha asc";
$resultado = mysql_query($sql) or die ($con->error_mysql("Error en la consulta".mysql_error()));
$fila = mysql_num_rows($resultado);
$con->cerrarBD();
//factura dia
$con = new Conexion();
$con2 = $con->conexionBD("fpproduccion");
$sql = "select 
		date_format(FacDiaFecha,'%d/%m/%Y') as fecha,
		FacDiaNumFac,
		clirsocial,
		facturadia.CliRut,
		FacDiaEstado,
		FacDiaNeto,
		FacDiaIVA,
		FacDiaMonto
		
		from facturadia
		inner join clientes on
		clientes.CliRut=facturadia.CliRut
		where month(FacDiaFecha)='".$cmb_Mes."' and year(FacDiaFecha)='".$cmb_Agno."' and FacDiaEstado!='ANULADA'
		order by FacDiaFecha asc";
$resultado2 = mysql_query($sql) or die ($con->error_mysql("Error en la consulta".mysql_error()));
$fila2 = mysql_num_rows($resultado2);
$con->cerrarBD();
//factura infa
$con = new Conexion();
$con2 = $con->conexionBD("fpproduccion");
$sql = "select 
		date_format(FacInfaFecha,'%d/%m/%Y') as fecha,
		FacInfaNumFac,
		clirsocial,
		facturainfa.CliRut,
		FacInfaEstado,
		FacInfaNeto,
		FacInfaIVA,
		FacInfaMonto
		
		from facturainfa
		inner join clientes on
		clientes.CliRut=facturainfa.CliRut
		where month(FacInfaFecha)='".$cmb_Mes."' and year(FacInfaFecha)='".$cmb_Agno."' and FacInfaEstado!='ANULADA'
		order by FacInfaFecha asc";
$resultado3 = mysql_query($sql) or die ($con->error_mysql("Error en la consulta".mysql_error()));
$fila3 = mysql_num_rows($resultado3);
$con->cerrarBD();
//factura otros
$con = new Conexion();
$con2 = $con->conexionBD("fpproduccion");
$sql = "select 
		date_format(FacOtrosFecha,'%d/%m/%Y') as fecha,
		FacOtrosNumFac,
		oclirsocial,
		facturaotros.OCliRut,
		FacOtrosEstado,
		FacOtrosNeto,
		FacOtrosIVA,
		FacOtrosMonto
		
		from facturaotros
		inner join otrosclientes on
		otrosclientes.OCliRut=facturaotros.OCliRut
		where month(FacOtrosFecha)='".$cmb_Mes."' and year(FacOtrosFecha)='".$cmb_Agno."' and FacOtrosEstado!='ANULADA'
		order by FacOtrosFecha asc";
$resultado4 = mysql_query($sql) or die ($con->error_mysql("Error en la consulta".mysql_error()));
$fila4 = mysql_num_rows($resultado4);
$con->cerrarBD();
//factura exenta
$con = new Conexion();
$con2 = $con->conexionBD("fpproduccion");
$sql = "select 
		date_format(FECliFecha,'%d/%m/%Y') as fecha,
		FECliNum,
		oclirsocial,
		fe.OCliRut,
		FECliEstado,
		FECliTotal
		
		from facexecliente fe
		inner join otrosclientes oc on
		oc.OCliRut=fe.OCliRut
		where month(FECliFecha)='".$cmb_Mes."' and year(FECliFecha)='".$cmb_Agno."' and FECliEstado!='ANULADA'
		order by FECliFecha asc";
$resultado5 = mysql_query($sql) or die ($con->error_mysql("Error en la consulta".mysql_error()));
$fila5 = mysql_num_rows($resultado5);
$con->cerrarBD();
//nota de credito
$con = new Conexion();
$con2 = $con->conexionBD("fpproduccion");
$sql = "select 
		date_format(NotCreFecha,'%d/%m/%Y') as fecha,
		NotCreNum,
		NotCreRSocial,
		NotCreRut,
		NotCreNeto
		
		from notacredito
		where month(NotCreFecha)='".$cmb_Mes."' and year(NotCreFecha)='".$cmb_Agno."'
		order by NotCreFecha asc";
$resultado6 = mysql_query($sql) or die ($con->error_mysql("Error en la consulta".mysql_error()));
$fila6 = mysql_num_rows($resultado6);
$con->cerrarBD();
if(($fila >0)or($fila2>0)or($fila3>0)or($fila4>0)or($fila5>0)or($fila6>0))
{
?>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="uno" id="tbExportar">
<thead>
<tr>
	<td colspan="8"><input name="txtMes" id="txtMes" type="hidden" value="<?php echo $cmb_Mes;?>" /><input name="txtAgno" id="txtAgno" type="hidden" value="<?php echo $cmb_Agno;?>" /><input type="hidden" id="datos_a_enviar" name="datos_a_enviar" /><div class='option2'>
  <label style="font:Verdana, Arial, Helvetica, sans-serif; color:#0066CC; font-weight:bold;">Reporte Mes <?php echo $mes." ".$cmb_Agno?></label>
  <div id="barras">
  	<div class="cuadroNormal">&nbsp;</div>&nbsp;
		<label>Factura PSMB</label>&nbsp;&nbsp;&nbsp;&nbsp;
	<div class="cuadroExenta">&nbsp;</div>&nbsp;
		<label>Factura DIA</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<div class="cuadroElectronica">&nbsp;</div>&nbsp;
		<label>Factura INFA</label>&nbsp;&nbsp;&nbsp;&nbsp;
	<div class="espacio"><img src="imagen/icono-excel.gif" width="20" height="20" title="Exportar a Excel" name="exportar" id="exportar" style="cursor: pointer; border: 1px solid green;" onmouseover="this.style.background='black';" onmouseout="this.style.background=''" />&nbsp;&nbsp;<img src="imagen/icono_grafico.gif" width="20" height="20" title="Grafico" name="grafico" id="grafico" style="cursor: pointer; border: 1px solid green;" onmouseover="this.style.background='black';" onmouseout="this.style.background=''" /></div>
  </div>
  <div id="barras">
  	<div class="cuadroOtros">&nbsp;</div>&nbsp;
		<label>Factura Otros</label>&nbsp;&nbsp;&nbsp;&nbsp;
	<div class="cuadroCompra">&nbsp;</div>&nbsp;
		<label>Factura Exenta</label>&nbsp;&nbsp;&nbsp;&nbsp;
	<div class="cuadroCredito">&nbsp;</div>&nbsp;
		<label>Nota Credito</label>&nbsp;&nbsp;&nbsp;&nbsp;
  </div>
</div></td>
</tr>
</thead>
</table>

<div id="contenido" style="display:block;">
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="uno">
<thead>
<tr>
	<th width="11%">fecha emision</th>
	<th width="9%">factura</th>
	<th width="32%">cliente</th>
	<th width="12%">rut</th>
	<th width="8%">estado</th>
	<th width="9%">neto $</th>
	<th width="9%">iva $</th>
	<th width="10%">total $</th>
</tr>
</thead>
<?php
$sumatoria = 0;
$sumaIVA = 0;
$sumaNeto = 0;
//factura psmb
while($reg = mysql_fetch_array($resultado))
{
	if($reg['FacEstado']=='NO PAGADO')
	{
		$estado = "<label style='color=red;font-weight:bold;'>NO PAGADO</label>";
	}
	else
	{	
		if($reg['FacEstado']=='ANULADA')
		{	$estado = "<label style='color=yellow;font-weight:bold;'>ANULADA</label>";}
		else
		{	$estado = "<label style='color=blue;font-weight:bold;'>PAGADO</label>";}
	}
	
	echo "<tr>";
	echo "<td width=\"11%\" class='facNormal'>".$reg['fecha']."</td>";
	echo "<td width=\"9%\" class='facNormal'>".$reg['FacCod']."</td>";
	echo "<td width=\"32%\" class='facNormal'>".$reg['clirsocial']."</td>";
	echo "<td width=\"12%\" class='facNormal'>".$reg['clirut']."</td>";
	echo "<td width=\"8%\" class='facNormal'>".$estado."</td>";
	echo "<td width=\"9%\" class='facNormal' style='text-align:right;'>".number_format ($reg['FacNeto'],0,',','.')."</td>";
	echo "<td width=\"9%\" class='facNormal' style='text-align:right;'>".number_format ($reg['FacIva'],0,',','.')."</td>";
	echo "<td width=\"10%\" class='facNormal' style='text-align:right;'>".number_format ($reg['FacTotal'],0,',','.')."</td>";
	echo "</tr>";
	$sumatoria += $reg['FacTotal'];
	$sumaIVA += $reg['FacIva'];
	$sumaNeto += $reg['FacNeto'];
}
//factura dia
while($reg = mysql_fetch_array($resultado2))
{		
	if($reg['FacDiaEstado']=='NO PAGADO')
	{
		$estado = "<label style='color=red;font-weight:bold;'>NO PAGADO</label>";
	}
	else
	{	
		if($reg['FacDiaEstado']=='ANULADA')
		{	$estado = "<label style='color=yellow;font-weight:bold;'>ANULADA</label>";}
		else
		{	$estado = "<label style='color=blue;font-weight:bold;'>PAGADO</label>";}
	}
	echo "<tr>";
	echo "<td width=\"11%\" class='facExenta'>".$reg['fecha']."</td>";
	echo "<td width=\"9%\" class='facExenta'>".$reg['FacDiaNumFac']."</td>";
	echo "<td width=\"32%\" class='facExenta'>".$reg['clirsocial']."</td>";
	echo "<td width=\"12%\" class='facExenta'>".$reg['CliRut']."</td>";
	echo "<td width=\"8%\" class='facExenta'>".$estado."</td>";
	echo "<td width=\"9%\" class='facExenta' style='text-align:right;'>".number_format ($reg['FacDiaNeto'],0,',','.')."</td>";
	echo "<td width=\"9%\" class='facExenta' style='text-align:right;'>".number_format ($reg['FacDiaIVA'],0,',','.')."</td>";
	echo "<td width=\"10%\" class='facExenta' style='text-align:right;'>".number_format ($reg['FacDiaMonto'],0,',','.')."</td>";
	echo "</tr>";
	$sumatoria += $reg['FacDiaMonto'];
	$sumaIVA += $reg['FacDiaIVA'];
	$sumaNeto += $reg['FacDiaNeto'];
}
//factura infa
while($reg = mysql_fetch_array($resultado3))
{
	if($reg['FacInfaEstado']=='NO PAGADO')
	{
		$estado = "<label style='color=red;font-weight:bold;'>NO PAGADO</label>";
	}
	else
	{	
		if($reg['FacInfaEstado']=='ANULADA')
		{	$estado = "<label style='color=yellow;font-weight:bold;'>ANULADA</label>";}
		else
		{	$estado = "<label style='color=blue;font-weight:bold;'>PAGADO</label>";}
	}
	echo "<tr>";
	echo "<td width=\"11%\" class='facElectronica'>".$reg['fecha']."</td>";
	echo "<td width=\"9%\" class='facElectronica'>".$reg['FacInfaNumFac']."</td>";
	echo "<td width=\"32%\" class='facElectronica'>".$reg['clirsocial']."</td>";
	echo "<td width=\"12%\" class='facElectronica'>".$reg['CliRut']."</td>";
	echo "<td width=\"8%\" class='facElectronica'>".$estado."</td>";
	echo "<td width=\"9%\" class='facElectronica' style='text-align:right;'>".number_format ($reg['FacInfaNeto'],0,',','.')."</td>";
	echo "<td width=\"9%\" class='facElectronica' style='text-align:right;'>".number_format ($reg['FacInfaIVA'],0,',','.')."</td>";
	echo "<td width=\"10%\" class='facElectronica' style='text-align:right;'>".number_format ($reg['FacInfaMonto'],0,',','.')."</td>";
	echo "</tr>";
	$sumatoria += $reg['FacInfaMonto'];
	$sumaIVA += $reg['FacInfaIVA'];
	$sumaNeto += $reg['FacInfaNeto'];
}
//factura otros
while($reg = mysql_fetch_array($resultado4))
{
	if($reg['FacOtrosEstado']=='NO PAGADO')
	{
		$estado = "<label style='color=red;font-weight:bold;'>NO PAGADO</label>";
	}
	else
	{	
		if($reg['FacOtrosEstado']=='ANULADA')
		{	$estado = "<label style='color=yellow;font-weight:bold;'>ANULADA</label>";}
		else
		{	$estado = "<label style='color=blue;font-weight:bold;'>PAGADO</label>";}
	}
	echo "<tr>";
	echo "<td width=\"11%\" class='facOtros'>".$reg['fecha']."</td>";
	echo "<td width=\"9%\" class='facOtros'>".$reg['FacOtrosNumFac']."</td>";
	echo "<td width=\"32%\" class='facOtros'>".$reg['oclirsocial']."</td>";
	echo "<td width=\"12%\" class='facOtros'>".$reg['OCliRut']."</td>";
	echo "<td width=\"8%\" class='facOtros'>".$estado."</td>";
	echo "<td width=\"9%\" class='facOtros' style='text-align:right;'>".number_format ($reg['FacOtrosNeto'],0,',','.')."</td>";
	echo "<td width=\"9%\" class='facOtros' style='text-align:right;'>".number_format ($reg['FacOtrosIVA'],0,',','.')."</td>";
	echo "<td width=\"10%\" class='facOtros' style='text-align:right;'>".number_format ($reg['FacOtrosMonto'],0,',','.')."</td>";
	echo "</tr>";
	$sumatoria += $reg['FacOtrosMonto'];
	$sumaIVA += $reg['FacOtrosIVA'];
	$sumaNeto += $reg['FacOtrosNeto'];
}

//factura exenta
while($reg = mysql_fetch_array($resultado5))
{
	if($reg['FECliEstado']=='FACTURADO')
	{	$estado = "<label style='color=red;font-weight:bold;'>NO PAGADO</label>";}
	else
	{	
		if($reg['FECliEstado']=='ANULADA')
		{	$estado = "<label style='color=yellow;font-weight:bold;'>ANULADA</label>";}
		else
		{	$estado = "<label style='color=blue;font-weight:bold;'>PAGADO</label>";}
	}
	
	echo "<tr>";
	echo "<td width=\"11%\" class='facCompra'>".$reg['fecha']."</td>";
	echo "<td width=\"9%\" class='facCompra'>".$reg['FECliNum']."</td>";
	echo "<td width=\"32%\" class='facCompra'>".$reg['oclirsocial']."</td>";
	echo "<td width=\"12%\" class='facCompra'>".$reg['OCliRut']."</td>";
	echo "<td width=\"8%\" class='facCompra'>".$estado."</td>";
	echo "<td width=\"9%\" class='facCompra' style='text-align:right;'>".number_format ($reg['FECliTotal'],0,',','.')."</td>";
	echo "<td width=\"9%\" class='facCompra' style='text-align:right;'>0</td>";
	echo "<td width=\"10%\" class='facCompra' style='text-align:right;'>".number_format ($reg['FECliTotal'],0,',','.')."</td>";
	echo "</tr>";
	$sumatoria += $reg['FECliTotal'];
	$sumaNeto += $reg['FECliTotal'];
}

//nota de credito
while($reg = mysql_fetch_array($resultado6))
{	
	$iva = round($reg['NotCreNeto']*0.19);
	$total = $iva+$reg['NotCreNeto'];
	echo "<tr>";
	echo "<td width=\"11%\" class='notaCredito'>".$reg['fecha']."</td>";
	echo "<td width=\"9%\" class='notaCredito'>".$reg['NotCreNum']."</td>";
	echo "<td width=\"32%\" class='notaCredito'>".$reg['NotCreRSocial']."</td>";
	echo "<td width=\"12%\" class='notaCredito'>".$reg['NotCreRut']."</td>";
	echo "<td width=\"8%\" class='notaCredito'>--</td>";
	echo "<td width=\"9%\" class='notaCredito' style='text-align:right;'>".number_format ($reg['NotCreNeto'],0,',','.')."</td>";
	echo "<td width=\"9%\" class='notaCredito' style='text-align:right;'>".number_format ($iva,0,',','.')."</td>";
	echo "<td width=\"10%\" class='notaCredito' style='text-align:right;'>".number_format ($total,0,',','.')."</td>";
	echo "</tr>";
	$sumatoria -= $total;
	$sumaNeto -= $reg['NotCreNeto'];
	$sumaIVA -= $iva;
}
?>
<tr>
	<td colspan="5" class="filaSumatoria">TOTAL</td>
	<td class="filaSumatoria" style='text-align:right;'><?php echo number_format ($sumaNeto,0,',','.')?></td>
	<td class="filaSumatoria" style='text-align:right;'><?php echo number_format ($sumaIVA,0,',','.')?></td>
	<td class="filaSumatoria" style='text-align:right;'><?php echo number_format ($sumatoria,0,',','.')?></td>
</tr>
</table>
</div>
<?php 
}
else
{
    echo "<label style='font-size:16px; color:#CC3333; font-weight:bold;'>No Existen Resultados para la Busqueda</label>";
}
?>
<table>
<tr><td><div id="mensaje"></div></td></tr>
</table>
</form>
</body>
<script>
$('#grafico').click(function() 
{ 
	 $.ajax({
            type: 'POST',
            url: 'Reportes/Detalle_Balance_Grafico_Venta.php',
            data: $('#form_compra').serialize(),
            success: function(data) {
                $('#contenido').html(data);
            }
        })
});
</script>
</html>
